package GUI;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import conference.Conference;
import conference.ConferenceIO;
import conference.TextParse;
import papers.Manuscript;
import papers.Manuscript.Status;

/**
 * A panel that allow to change the manuscript status.
 * @author Dickson Kwong
 * @version v1.2 Nov. 30, 2011
 */

@SuppressWarnings("serial")
public class PCChangeStatusPanel extends JPanel {
	/**
	 * The PCChangeStatusPanel Constructor. It creates 
	 * a panel to change manuscript status.
	 * @param the_manuscript The manuscript
	 * @param the_conference The conference
	 */
	public PCChangeStatusPanel(final Manuscript the_manuscript, 
			final Conference the_conference) {
	    super(true);
	    setLayout(new FlowLayout());
	    setSize(200, 600);
	    final StringBuilder sb = new StringBuilder();
	    
	    sb.append("Confirm status change?\nFrom " + 
    			the_manuscript.getStatus().toString() + " to Yes");
	    if(the_manuscript.getReviews().size() != 
	    	the_manuscript.getReviewers().size()){
	    	sb.append("\n\nNot all reviewers have submitted their reviews!");
	    }
	    
	    //Create a action listener for the Yes radio button.
	    Action action1 = new AbstractAction("Yes") {
	        public void actionPerformed(ActionEvent evt) {
	        	if(JOptionPane.showConfirmDialog(null, sb.toString(), "Change Status", 
	        			JOptionPane.OK_CANCEL_OPTION) == 0){
					JOptionPane.showMessageDialog(null, "Status changed to Yes",
					"Changed", 1);
					the_manuscript.setStatus(Status.YES);
					ConferenceIO.saveConference(TextParse.getFileLoc(), the_conference);
	        	} else
	        		JOptionPane.showMessageDialog(null, "Cancel",
	        				"Cancel", 1);
        	}  
	    };
	    
	    final StringBuilder sb1 = new StringBuilder();
	    
	    sb1.append("Confirm status change?\nFrom " + 
    			the_manuscript.getStatus().toString() + " to No");
	    if(the_manuscript.getReviews().size() != the_manuscript.getReviewers().size()){
	    	sb1.append("\n\nNot all reviewers have submitted their reviews!");
	    }

	    //Create a action listener for the No radio button.
	    Action action2 = new AbstractAction("No") {
	        public void actionPerformed(ActionEvent evt) {
	        	if(JOptionPane.showConfirmDialog(null, sb1.toString(), "Change Status", 
	        			JOptionPane.OK_CANCEL_OPTION) == 0){
					JOptionPane.showMessageDialog(null, "Status changed to No",
					"Changed", 1);
					the_manuscript.setStatus(Status.NO);
					ConferenceIO.saveConference(TextParse.getFileLoc(), the_conference);
	        	} else
	        		JOptionPane.showMessageDialog(null, "Cancel",
	        				"Cancel", 1);
        	}  
	    };
	    
	    final StringBuilder sb2 = new StringBuilder();
	    
	    sb2.append("Confirm status change?\nFrom " + 
    			the_manuscript.getStatus().toString() + " to undecided");
	    if(the_manuscript.getReviews().size() != the_manuscript.getReviewers().size()){
	    	sb2.append("\n\nNot all reviewers have submitted their reviews!");
	    }
	    
	    //Create a action listener for the Undecided radio button.
		Action action3 = new AbstractAction("Undecided") {
		        public void actionPerformed(ActionEvent evt) {
		        	if(JOptionPane.showConfirmDialog(null, sb2.toString(), "Change Status", 
		        			JOptionPane.OK_CANCEL_OPTION) == 0){
						JOptionPane.showMessageDialog(null, "Status changed to undecided",
						"Changed", 1);
						the_manuscript.setStatus(Status.UNDECIDED);
						ConferenceIO.saveConference(TextParse.getFileLoc(), the_conference);
		        	} else
		        		JOptionPane.showMessageDialog(null, "Cancel",
		        				"Cancel", 1);
	        	}
		};

		//Assigned the action to each button.
		JRadioButton b1 = new JRadioButton(action1);
		JRadioButton b2 = new JRadioButton(action2);
		JRadioButton b3 = new JRadioButton(action3);
		
		//Set the font for the buttons.
		Font f = new Font("Dialog", Font.PLAIN, 24);
		b1.setFont(f);
		b2.setFont(f);
		b3.setFont(f);
		
		if(the_manuscript.getStatus() == Manuscript.Status.YES){
			b1.setSelected(true);
		}else if (the_manuscript.getStatus() == Manuscript.Status.NO){
			b2.setSelected(true);
		}else{
			b3.setSelected(true);
		}

		//Assigned all the buttons to the button group.
		ButtonGroup group = new ButtonGroup();
		group.add(b1);
		group.add(b2);
		group.add(b3);

		//Add all the buttons to the panel
		add(b1);
		add(b2);
		add(b3);
	}
}
